// 赋值运算符重载
#include <iostream>
using namespace std;
class Person {
public:
    int* m_age;
    Person();
    Person(int age);
    ~Person();
    Person& operator=(const Person &p) {
        // 编译器提供浅拷贝
        // this->m_age = p.m_age;

        // 先判断自己是否有属性在堆区
        if (this->m_age != nullptr) {
            delete this->m_age;
            this->m_age = nullptr;
        }
        // 深拷贝
        this->m_age = new int(*p.m_age);
        // 出于链式编程考虑返回对象
        return *this;
    }
};
Person::Person() {}
Person::Person(int age) {
    this->m_age = new int(age);
}
Person::~Person() {
    cout << "释放" << endl;
    if (this->m_age != nullptr) {
        delete this->m_age;
        this->m_age = nullptr;
    }
}
void test1() {
    Person p1(20);
    Person p2(40);
    Person p3(60);
    p3 = p2 = p1;
    cout << "p1.age = " << *p1.m_age << endl;
    cout << "p2.age = " << *p2.m_age << endl;
    cout << "p3.age = " << *p3.m_age << endl;
}
int main(void) {
    test1();

    return 0;
}